<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!--手机 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input
              v-model="userInfo.mobile"
            />

          </el-form-item>

        </el-col>
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- <upload-img ref="staffPhoto" /> -->
            <!-- ref="photoxs" -->
            <!-- v-model='test' -->
            <Elupload v-model="test">
              <!-- <img v-if="imageUrl" :src="imageUrl" class="avatar"> -->
              <!-- <i v-else class="el-icon-plus avatar-uploader-icon" /> -->
            </Elupload>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSaveUserDetailById">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employess'
export default {

  data() {
    return {
      test: '',
      userInfo: {
        id: this.$route.query.id,
        mobile: '',
        timeOfEntry: ''
      }
    }
  },
  mounted() {
    // this.getUserDetailById()
    this.loadUserDatailById()
    // this.hSaveUserDetailById()
  },
  methods: {

    upload(params) {
      console.log(params)
    },
    async loadUserDatailById() {
      const res = await getUserDetailById(this.userInfo.id)
      console.log('获取个人信息', res)
      this.userInfo = res.data
      // eslint-disable-next-line no-prototype-builtins
      if (res.data.hasOwnProperty('staffPhoto') === false) {
        res.data.staffPhoto = ''
      }
      // 把头像地址 给子组件显示出来
      this.test = res.data.staffPhoto
    },
    async hSaveUserDetailById() {
      try {
        // 头像上传是在子组件 uploadImg中实现，上传成功之后新的头像的地址
        // 也保存在子组件中

        // 这里需要从子组件中获取 新的头像的地址， 调用接口
        // const newPhotoUrl = this.$refs.photoxs.imageUrl
        // console.log('从子组件中获取 新的头像的地址', newPhotoUrl)
        this.userInfo.staffPhoto = this.test

        const res = await saveUserDetailById(this.userInfo)
        console.log('保存个人信息之后的结果是', res)
        // 1. 提示
        this.$message.success('保存成功')
        // 2. 回到列表页
        this.$router.back()
        console.log(this.test)
      } catch (err) {
        this.$message.error(err.message)
      }
    }

  }
}
</script>

<style scoped lang="scss">
.progress {
  position: absolute;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #fff;
}
  .user-info{
    padding-top:20px;
  }
</style>
